<?php
/**
 * 用户账号管理
 *
 * @author kaikaic
 * @date 2018/04/09
 *
 */

namespace app\exam\controller;

use app\exam\service\AccountService;
use controller\BasicAdmin;
use service\ConfigService;
use service\ExcelService;
use think\Db;

class Account extends BasicAdmin {

    protected $table = "Account";

	public function index() {
		$this->title = "账号列表";
        $query = array();
        $query['mobile'] = $this->request->get("mobile");
        $query['nick_name'] = $this->request->get("nick_name");
        $query['status'] = intval($this->request->get("status"));

        $db = Db::name($this->table);
        if ($query['mobile']) {
            $db->where(['mobile'=>array('like', "%{$query['mobile']}%")]);
        }
        $db->order('id desc');

        return parent::_list($db);
	}

    public function demo() {

        AccountService::createUser("赵鹏程", 13900119966);
    }

	/**
	 * 列表数据处理
	 */
	protected function _index_data_filter(&$data) {
		$size = count($data);
		$cfgSex = ConfigService::cfgSex();
		$cfgStatus = ConfigService::cfgAccountStatus();
		for ($i=0; $i<$size; $i++) {
			$data[$i]['sex'] = $cfgSex[$data[$i]['gender']];
			$data[$i]['status_txt'] = $cfgStatus[$data[$i]['status']];

            $data[$i]['is_talent'] = 0;
		}

		$this->assign('cfg_status', $cfgStatus);
	}

    /**
     * 编辑-会员
     */
    public function edit() {
        $id = $this->request->param('id', '');
        if (empty($id)) {
            $this->error('参数错误，请稍候再试！');
        }

        if ($this->request->isGet()) {
            $account = AccountService::getInstance()->find($id);
            if (!$account) {
                $this->error('参数错误，用户不存在！');
            }
            return view('form', ['title'=>'编辑用户', 'vo'=>$account]);
        }

        $data = $this->request->post();
        if (!empty($id)) {
            if (false !== AccountService::getInstance()->update($data, $id)) {
                $this->success('编辑成功!', '');
            }
        }
        $this->error('编辑失败，请稍候再试！');
    }

    /**
     * 删除 - 会员
     */
    public function del() {
        $id = $this->request->param('id', '');
        if (empty($id)) {
            $this->error('参数错误，请稍候再试！');
        }
        if (AccountService::delete($id)) {
            $this->success("会员屏蔽成功！", '');
        }
        $this->error("会员屏蔽失败，请稍候再试！");
    }

    public function export() {
        ExcelService::exportExcel();
    }
}

